﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using WpfCustomControlLibrary;
using System.Reflection;
using System.Data;
using System.Windows.Data;
using SSPharma.Common;

namespace SSPharma.Common
{
    class UtilityUIElement
    {
        public static string GetValueFromDT(DataTable dt, string ID, string fieldID, string fieldName)
        {
            try
            {
                string str = "";
                string str2 = "";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    str  = dt.Rows[i][fieldID].ToString();
                    if (str == ID)
                    {
                        str2 = dt.Rows[i][fieldName].ToString();
                        break;
                    }
                }
                return str2;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Xóa trắng dữ liệu trên các controll
        /// </summary>
        /// <param name="aUIRoot"></param>
        public static void BlankControlText(UIElementCollection aUIRoot)
        {
            int count = aUIRoot.Count;
            for (int i = 0; i < count; i++)
            {
                try
                {
                    string str;
                    UIElement element = aUIRoot[i];
                    Type type = element.GetType();
                    if (type == typeof(TextBox))
                    {
                        str = ((TextBox)element).Name.Substring(3);
                        ((TextBox)element).Text = "";
                    }
                    else if (type == typeof(AWTextBox))
                    {
                        str = ((AWTextBox)element).Name.Substring(3);
                        ((AWTextBox)element).Text ="0";
                    }
                    else if (type == typeof(StackPanel))
                    {
                        BlankControlText(((StackPanel)element).Children);
                    }
                    else if (type == typeof(AWDatePicker))
                    {
                        str = ((AWDatePicker)element).Name.Substring(3);
                        ((AWDatePicker)element).Text = ((AWDatePicker)element).FormatDate;
                    }
                    else if (type == typeof(CheckBox))
                    {
                        str = ((CheckBox)element).Name.Substring(3);
                        ((CheckBox)element).IsChecked = false;
                    }
                    else if (type == typeof(ComboBox))
                    {
                        str = ((ComboBox)element).Name.Substring(3);
                        ((ComboBox)element).SelectedIndex = 0;
                    }
                    else if (type == typeof(ListBox))
                    {
                        str = ((ListBox)element).Name.Substring(3);
                        ((ListBox)element).SelectedIndex = 0;
                    }
                    else if (type == typeof(PasswordBox))
                    {
                        str = ((PasswordBox)element).Name.Substring(3);
                        ((PasswordBox)element).Password = "";
                    }
                }
                catch
                {
                }
            }
        }

        public static void ChangeFocus(object sender, KeyEventArgs e, Grid LayoutRoot)
        {
            if (e.Key == Key.Enter)
            {
                Type type = sender.GetType();
                int tabIndex = 0;
                if (type == typeof(TextBox))
                {
                    tabIndex = ((TextBox)sender).TabIndex;
                }
                else if (type == typeof(DatePicker))
                {
                    tabIndex = ((DatePicker)sender).TabIndex;
                }
                else if (type == typeof(CheckBox))
                {
                    tabIndex = ((CheckBox)sender).TabIndex;
                }
                else if (type == typeof(ComboBox))
                {
                    tabIndex = ((ComboBox)sender).TabIndex;
                }
                else if (type == typeof(Button))
                {
                    tabIndex = ((Button)sender).TabIndex;
                }
                int num2 = ControlIndexMax(LayoutRoot.Children);
                if (tabIndex == ControlIndexMax(LayoutRoot.Children))
                {
                    tabIndex = -1;
                }
                if (LayoutRoot.Children.Count != 2)
                {
                    for (int i = tabIndex + 1; i <= 200; i++)
                    {
                        if (IsFocusControl(IndexToControl(i, LayoutRoot.Children)))
                        {
                            break;
                        }
                    }
                }
            }
        }

        public static int ControlIndexMax(UIElementCollection aUIRoot)
        {
            int num = -1;
            int count = aUIRoot.Count;
            try
            {
                for (int i = 0; i < count; i++)
                {
                    UIElement element = aUIRoot[i];
                    Type type = element.GetType();
                    int num4 = -1;
                    if (type == typeof(StackPanel))
                    {
                        num4 = ControlIndexMax(((StackPanel)element).Children);
                    }
                    else
                    {
                        num4 = ControlIndex(i, aUIRoot);
                    }
                    if (num4 > num)
                    {
                        num = num4;
                    }
                }
            }
            catch
            {
                num = -1;
            }
            return num;
        }

        public static int ControlIndex(int aiIndex, UIElementCollection aUIRoot)
        {
            UIElement element = aUIRoot[aiIndex];
            Type type = element.GetType();
            if (type == typeof(TextBox))
            {
                return ((TextBox)element).TabIndex;
            }
            if (type == typeof(PasswordBox))
            {
                return ((PasswordBox)element).TabIndex;
            }
            if (type == typeof(ComboBox))
            {
                return ((ComboBox)element).TabIndex;
            }
            if (type == typeof(DatePicker))
            {
                return ((DatePicker)element).TabIndex;
            }
            if (type == typeof(CheckBox))
            {
                return ((CheckBox)element).TabIndex;
            }
            if (type == typeof(Button))
            {
                return ((Button)element).TabIndex;
            } 
            return -1;
        }

        public static bool IsFocusControl(UIElement luiControl)
        {
            Type type = luiControl.GetType();
            if (type.ToString().Trim() != "System.Windows.Controls.TextBlock")
            {
                if (type == typeof(TextBox))
                {
                    if (!((TextBox)luiControl).IsEnabled)
                    {
                        return false;
                    }
                    ((TextBox)luiControl).Focus();
                    ((TextBox)luiControl).SelectionLength = ((TextBox)luiControl).Text.Length;
                    return true;
                }
                if (type == typeof(PasswordBox))
                {
                    if (!((PasswordBox)luiControl).IsEnabled)
                    {
                        return false;
                    }
                    ((PasswordBox)luiControl).Focus();
                    return true;
                }
                if (type == typeof(ComboBox))
                {
                    if (!((ComboBox)luiControl).IsEnabled)
                    {
                        return false;
                    }
                    ((ComboBox)luiControl).Focus();
                    return true;
                }
                if (type == typeof(DatePicker))
                {
                    ((DatePicker)luiControl).Focus();
                    if (!((DatePicker)luiControl).IsEnabled)
                    {
                        return false;
                    }
                    return true;
                }
                if (type == typeof(CheckBox))
                {
                    if (!((CheckBox)luiControl).IsEnabled)
                    {
                        return false;
                    }
                    ((CheckBox)luiControl).Focus();
                    return true;
                }
                if (type == typeof(Button))
                {
                    if (!((Button)luiControl).IsEnabled)
                    {
                        return false;
                    }
                    ((Button)luiControl).Focus();
                    return true;
                }
            }
            return false;
        }

        public static UIElement IndexToControl(int aiIndex, UIElementCollection aUIRoot)
        {
            UIElement luiControl = new TextBlock();
            int count = aUIRoot.Count;
            try
            {
                for (int i = 0; i < count; i++)
                {
                    UIElement element2 = aUIRoot[i];
                    Type type = element2.GetType();
                    int controlIndex = -1;
                    if (type == typeof(StackPanel))
                    {
                        if (((StackPanel)element2).Visibility == Visibility.Collapsed)
                        {
                            continue;
                        }
                        luiControl = IndexToControl(aiIndex, ((StackPanel)element2).Children);
                        controlIndex = GetControlIndex(luiControl);
                        if (controlIndex == aiIndex)
                        {
                            return luiControl;
                        }
                    }
                    else
                    {
                        controlIndex = ControlIndex(i, aUIRoot);
                    }
                    if (controlIndex == aiIndex)
                    {
                        return aUIRoot[i];
                    }
                }
            }
            catch
            {
                luiControl = new TextBlock();
            }
            return luiControl;
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Enter tabindex
        /// </summary>
        /// <param name="luiControl"></param>
        /// <returns></returns>
        private static int GetControlIndex(UIElement luiControl)
        {
            Type type = luiControl.GetType();
            if (type == typeof(TextBox))
            {
                return ((TextBox)luiControl).TabIndex;
            }
            if (type == typeof(PasswordBox))
            {
                return ((PasswordBox)luiControl).TabIndex;
            }
            if (type == typeof(ComboBox))
            {
                return ((ComboBox)luiControl).TabIndex;
            }
            if (type == typeof(DatePicker))
            {
                return ((DatePicker)luiControl).TabIndex;
            }
            if (type == typeof(CheckBox))
            {
                return ((CheckBox)luiControl).TabIndex;
            }
            if (type == typeof(Button))
            {
                return ((Button)luiControl).TabIndex;
            }
           
            return -1;
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Hàm dùng để ẩn hiện các controll trên form
        /// </summary>
        /// <param name="isEnable"></param>
        /// <param name="aUIRoot"></param>
        public static void Enable_Control(bool isEnable, UIElementCollection aUIRoot)
        {
            int count = aUIRoot.Count;
            for (int i = 0; i < count; i++)
            {
                try
                {
                    UIElement element = aUIRoot[i];
                    Type type = element.GetType();
                    if (type == typeof(TextBox))
                    {
                        ((TextBox)element).IsEnabled = isEnable;
                    }
                    else if (type == typeof(AWTextBox))
                    {
                        ((AWTextBox)element).IsEnabled = isEnable;
                    }
                    else if (type == typeof(StackPanel))
                    {
                        Enable_Control(isEnable, ((StackPanel)element).Children);
                    }
                    else if (type == typeof(AWDatePicker))
                    {
                        ((AWDatePicker)element).IsEnabled = isEnable;
                    }
                    else if (type == typeof(AWCboCheckBox))
                    {
                        ((AWCboCheckBox)element).IsEnabled = isEnable;
                    }
                    else if (type == typeof(AWTextBox))
                    {
                        ((AWTextBox)element).IsEnabled = isEnable;
                    }
                    else if (type == typeof(CheckBox))
                    {
                        ((CheckBox)element).IsEnabled = isEnable;
                    }
                    else if (type == typeof(ComboBox))
                    {
                        ((ComboBox)element).IsEnabled = isEnable;
                    }
                    else if (type == typeof(ListBox))
                    {
                        ((ListBox)element).IsEnabled = isEnable;
                    }
                    else if (type == typeof(DatePicker))
                    {
                        ((DatePicker)element).IsEnabled = isEnable;
                    }

                    else if (type == typeof(PasswordBox))
                    {
                        ((PasswordBox)element).IsEnabled = isEnable;
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// hàm lấy format grid
        /// </summary>
        /// <param name="Grid"></param>
        /// <param name="dtdata"></param>
        public static void GenerateColumns(DataGrid Grid, DataTable dtdata, ref IList<string> list, ref  IList<string> KhoaChinh)
        {
            Grid.Columns.Clear();
            //Style style1 = new Style
            //{
            //    TargetType = typeof(Control)
            //};
            //style1.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, "Right"));

            //Style style = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));
            //style.Setters.Add(new Setter(System.Windows.Controls.Primitives.DataGridColumnHeader.HorizontalContentAlignmentProperty, "Right"));
            foreach (DataRow dr in dtdata.Rows)
            {
                if (dr["TinhTrang"] != null && dr["TinhTrang"].ToString() == "True")
                {
                    if (dr["KieuDuLieu"] != null && dr["KieuDuLieu"].ToString() == "S")
                    {
                        Grid.Columns.Add(new DataGridTextColumn
                        {
                            Header = dr["TenHienThi"],
                            Width = new DataGridLength(Convert.ToInt32(dr["DoRong"])),
                            Binding = new Binding(dr["TenTruong"].ToString())
                        });
                    }
                    else if (dr["KieuDuLieu"] != null && dr["KieuDuLieu"].ToString() == "Do")
                    {
                        Grid.Columns.Add(new DataGridTextColumn
                        {
                            Header = dr["TenHienThi"],
                            Width = new DataGridLength(Convert.ToInt32(dr["DoRong"])),
                            Binding = dr["DinhDangSo"] != null ?
                            new Binding(dr["TenTruong"].ToString()) { Converter = new StringFormatConverter(), ConverterParameter = dr["DinhDangSo"].ToString() } : 
                            new Binding(dr["TenTruong"].ToString()) { Converter = new StringFormatConverter() }
                        });
                    }
                    else if (dr["KieuDuLieu"] != null && dr["KieuDuLieu"].ToString() == "B")
                    {
                        Grid.Columns.Add(new DataGridCheckBoxColumn
                        {
                            Header = dr["TenHienThi"],
                            Width = new DataGridLength(Convert.ToInt32(dr["DoRong"])),
                            Binding = new Binding(dr["TenTruong"].ToString())
                        });
                    }
                    if (dr["KhoaChinh"].ToString() == "True")
                        KhoaChinh.Add(dr["TenTruong"].ToString());
                    list.Add(dr["TenTruong"].ToString());

                }
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// hàm lấy format grid cho lưới group
        /// </summary>
        /// <param name="Grid"></param>
        /// <param name="dtdata"></param>
        public static void GenerateColumns(DataGrid Grid, DataTable dtdata)
        {
            Grid.Columns.Clear();
            foreach (DataRow dr in dtdata.Rows)
            {
                if (dr["TinhTrang"] != null && dr["TinhTrang"].ToString() == "True")
                {
                    if (dr["KieuDuLieu"] != null && dr["KieuDuLieu"].ToString() == "S")
                    {
                        Grid.Columns.Add(new DataGridTextColumn
                        {
                            Header = dr["TenHienThi"],
                            Width = new DataGridLength(Convert.ToInt32(dr["DoRong"])),
                            Binding = new Binding(dr["TenTruong"].ToString())
                        });
                    }
                    else if (dr["KieuDuLieu"] != null && dr["KieuDuLieu"].ToString() == "Do")
                    {
                        Grid.Columns.Add(new DataGridTextColumn
                        {
                            Header = dr["TenHienThi"],
                            Width = new DataGridLength(Convert.ToInt32(dr["DoRong"])),
                            Binding = dr["DinhDangSo"] != null ?
                            new Binding(dr["TenTruong"].ToString()) { Converter = new StringFormatConverter(), ConverterParameter = dr["DinhDangSo"].ToString() } :
                            new Binding(dr["TenTruong"].ToString()) { Converter = new StringFormatConverter() }
                        });
                    }
                    else if (dr["KieuDuLieu"] != null && dr["KieuDuLieu"].ToString() == "B")
                    {
                        Grid.Columns.Add(new DataGridCheckBoxColumn
                        {
                            Header = dr["TenHienThi"],
                            Width = new DataGridLength(Convert.ToInt32(dr["DoRong"])),
                            Binding = new Binding(dr["TenTruong"].ToString())
                        });
                    }
                }
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// hàm lấy dữ liệu cho các biến phục vụ việc khởi tạo lưới
        /// </summary>
        /// <param name="dtdata">dữ liệu</param>
        /// <param name="list">danh sách  các cột cần select</param>
        /// <param name="KhoaChinh">các cột khóa chính</param>
        public static void GenerateColumns(DataTable dtdata, ref IList<string> list, ref  IList<string> KhoaChinh)
        {
            foreach (DataRow dr in dtdata.Rows)
            {
                if (dr["TinhTrang"] != null && dr["TinhTrang"].ToString() == "True")
                {
                    if (dr["KhoaChinh"].ToString() == "True")
                        KhoaChinh.Add(dr["TenTruong"].ToString());
                    list.Add(dr["TenTruong"].ToString());
                }
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// gán giá trị cho các Properties trong model
        /// </summary>
        /// <typeparam name="Model"></typeparam>
        /// <param name="model"></param>
        /// <param name="TagName"></param>
        /// <param name="RootName"></param>
        public static void ElementModelToControl<Model>(ref Model model, string TagName, UIElementCollection RootName)
        {
            SetValueModelControl(ref model, TagName, RootName);
            int count = RootName.Count;
            for (int i = 0; i < count; i++)
            {
                try
                {
                    UIElement element = RootName[i];
                    if (element.GetType() == typeof(StackPanel))
                    {
                        SetValueModelControl(ref model, TagName, ((StackPanel)element).Children);
                    }
                }
                catch
                {
                }
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// set gia tri cho từng properties
        /// </summary>
        /// <typeparam name="Model"></typeparam>
        /// <param name="model"></param>
        /// <param name="astrTypeTable"></param>
        /// <param name="aUIRoot"></param>
        private static void SetValueModelControl<Model>(ref Model model, string astrTypeTable, UIElementCollection aUIRoot)
        {
            string str = "";
            int count = aUIRoot.Count;
            for (int i = 0; i < count; i++)
            {
                try
                {
                    string name;
                    UIElement element = aUIRoot[i];
                    Type type = element.GetType();
                    if (type == typeof(TextBox))
                    {
                        try
                        {
                            str = ((TextBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((TextBox)element).Name.Substring(3);
                            ((TextBox)element).Text = "";
                            PropertyInfo prop = model.GetType().GetProperty(name);
                            string values = prop.GetValue(model, null) != null ? prop.GetValue(model, null).ToString() : "";
                            ((TextBox)element).Text = values;
                        }
                    }
                    else if (type == typeof(AWTextBox))
                    {
                        try
                        {
                            str = ((AWTextBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((AWTextBox)element).Name;
                            string format = "";
                            name = name.Substring(3);
                            if (!((AWTextBox)element).aaIsTextBox)
                            {
                                format = ((AWTextBox)element).FormatNumber;
                                PropertyInfo prop = model.GetType().GetProperty(name);
                                string values = prop.GetValue(model, null) != null ? prop.GetValue(model, null).ToString() : "0";
                                ((AWTextBox)element).Text = Convert.ToDouble(values).ToString(format);
                            }
                            else
                            {
                                PropertyInfo prop = model.GetType().GetProperty(name);
                                string values = prop.GetValue(model, null) != null ? prop.GetValue(model, null).ToString() : "0";
                                ((AWTextBox)element).Text = values;
                            }
                        }
                    }
                    else if (type == typeof(TextBlock))
                    {
                        try
                        {
                            str = ((TextBlock)element).Tag.ToString();
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((TextBlock)element).Name.Substring(3);
                            ((TextBlock)element).Text = "";
                            PropertyInfo prop = model.GetType().GetProperty(name);
                            string values = prop.GetValue(model, null) != null ? prop.GetValue(model, null).ToString() : "";
                            ((TextBlock)element).Text = values;
                        }
                    }
                    else
                    {
                        if (type == typeof(AWDatePicker))
                        {
                            try
                            {
                                str = ((AWDatePicker)element).Tag.ToString();
                            }
                            catch
                            {
                                str = "";
                            }
                            if (str == astrTypeTable)
                            {
                                name = ((AWDatePicker)element).Name.Substring(3);
                                PropertyInfo prop = model.GetType().GetProperty(name);
                                string values = prop.GetValue(model, null) != null ? prop.GetValue(model, null).ToString() : "";
                                ((AWDatePicker)element).Text = values;
                            }
                        }
                        else if (type == typeof(CheckBox))
                        {
                            try
                            {
                                str = ((CheckBox)element).Tag.ToString();
                            }
                            catch
                            {
                                str = "";
                            }
                            if (str == astrTypeTable)
                            {
                                name = ((CheckBox)element).Name.Substring(3);
                                PropertyInfo prop = model.GetType().GetProperty(name);
                                string values = prop.GetValue(model, null) != null ? prop.GetValue(model, null).ToString() : "";
                                ((CheckBox)element).IsChecked = false;
                                if (values.ToUpper() == "TRUE")
                                {
                                    ((CheckBox)element).IsChecked = true;
                                }
                            }
                        }
                        else if (type == typeof(ComboBox))
                        {
                            try
                            {
                                str = ((ComboBox)element).Tag.ToString();
                            }
                            catch
                            {
                                str = "";
                            }
                            if (str == astrTypeTable)
                            {
                                name = ((ComboBox)element).Name.Substring(3);
                                PropertyInfo prop = model.GetType().GetProperty(name);
                                string values = prop.GetValue(model, null) != null ? prop.GetValue(model, null).ToString() : "";
                                ((ComboBox)element).SelectedValue = values;
                            }
                        }
                        else if (type == typeof(ListBox))
                        {
                            try
                            {
                                str = ((ListBox)element).Tag.ToString();
                            }
                            catch
                            {
                                str = "";
                            }
                            if (str == astrTypeTable)
                            {
                                name = ((ListBox)element).Name.Substring(3);
                                PropertyInfo prop = model.GetType().GetProperty(name);
                                string values = prop.GetValue(model, null) != null ? prop.GetValue(model, null).ToString() : "";
                                ((ListBox)element).SelectedValue = values;
                            }
                        }
                        else if (type == typeof(PasswordBox))
                        {
                            try
                            {
                                str = ((PasswordBox)element).Tag.ToString();
                            }
                            catch
                            {
                                str = "";
                            }
                            if (str == astrTypeTable)
                            {
                                name = ((PasswordBox)element).Name.Substring(3);
                                PropertyInfo prop = model.GetType().GetProperty(name);
                                string values = prop.GetValue(model, null) != null ? prop.GetValue(model, null).ToString() : "";
                                ((PasswordBox)element).Password = values;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// lấy dữ liệu từ các controll vao model
        /// </summary>
        /// <param name="drInfoName"></param>
        /// <param name="TagName"></param>
        /// <param name="LayoutRootName"></param>
        public static void ControlToElementModel<Model>(ref Model model, string TagName, UIElementCollection LayoutRootName)
        {
            GetValueModelFromControl(ref model, TagName, LayoutRootName);
            int count = LayoutRootName.Count;
            for (int i = 0; i < count; i++)
            {
                try
                {
                    UIElement element = LayoutRootName[i];
                    if (element.GetType() == typeof(StackPanel))
                    {
                        GetValueModelFromControl(ref model, TagName, ((StackPanel)element).Children);
                    }
                }
                catch
                {
                }
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// lay value cua mot controll dạng model
        /// </summary>
        /// <typeparam name="Model"></typeparam>
        /// <param name="model"></param>
        /// <param name="astrTypeTable"></param>
        /// <param name="aUIRoot"></param>
        private static void GetValueModelFromControl<Model>(ref Model model, string astrTypeTable, UIElementCollection aUIRoot)
        {
            int count = aUIRoot.Count;
            for (int i = 0; i < count; i++)
            {
                string name = "";
                try
                {
                    string str2;
                    UIElement element = aUIRoot[i];
                    Type type = element.GetType();
                    if (type == typeof(TextBox))
                    {
                        if (!string.IsNullOrEmpty(((TextBox)element).Tag.ToString()))
                        {
                            str2 = "";
                            try
                            {
                                str2 = ((TextBox)element).Tag.ToString();
                            }
                            catch
                            {
                                str2 = "";
                            }
                            if (str2 == astrTypeTable)
                            {
                                name = ((TextBox)element).Name;
                                name = name.Substring(3);
                                PropertyInfo prop = model.GetType().GetProperty(name);
                                string values = ((TextBox)element).Text;
                                prop.SetValue(model, values, null);
                            }
                        }
                    }
                    else if (type == typeof(ComboBox))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((ComboBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((ComboBox)element).Name;
                            name = name.Substring(3);
                            PropertyInfo prop = model.GetType().GetProperty(name);
                            object values = ((ComboBox)element).SelectedValue;
                            prop.SetValue(model, values, null);
                        }
                    }
                    else if (type == typeof(ListBox))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((ListBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((ListBox)element).Name;
                            name = name.Substring(3);
                            PropertyInfo prop = model.GetType().GetProperty(name);
                            object values = ((ListBox)element).SelectedValue;
                            prop.SetValue(model, values, null);
                        }
                    }
                    else if (type == typeof(TextBlock))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((TextBlock)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((TextBlock)element).Name;
                            name = name.Substring(3);
                            PropertyInfo prop = model.GetType().GetProperty(name);
                            string values = ((TextBlock)element).Text;
                            prop.SetValue(model, values, null);
                        }
                    }
                    else if (type == typeof(AWTextBox))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((AWTextBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((AWTextBox)element).Name;
                            name = name.Substring(3);
                            PropertyInfo prop = model.GetType().GetProperty(name);
                            string values = ((AWTextBox)element).Text;
                            prop.SetValue(model, values, null);
                        }
                    }
                    else if (type == typeof(DatePicker))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((DatePicker)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((DatePicker)element).Name;
                            name = name.Substring(3);
                            PropertyInfo prop = model.GetType().GetProperty(name);
                            string values = ((DatePicker)element).Text;
                            prop.SetValue(model, values, null);
                        }
                    }
                    else if (type == typeof(CheckBox))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((CheckBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((CheckBox)element).Name;
                            name = name.Substring(3);
                            string str3 = "false";
                            if (((CheckBox)element).IsChecked == true)
                            {
                                str3 = "true";
                            }
                            PropertyInfo prop = model.GetType().GetProperty(name);
                            prop.SetValue(model, str3, null);
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw new Exception(exception.Message + ": " + name);
                }
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Lấy user controll hiện tại trong form main
        /// </summary>
        /// <param name="isEnable"></param>
        /// <param name="contenControl"></param>
        public static UserControl GetUserControl(ContentControl contenControl, string nameControl)
        {
            try
            {
                Type type = contenControl.GetType();
                if (type == typeof(ContentControl))
                {
                    var child = System.Windows.Media.VisualTreeHelper.GetChild(contenControl, 0);
                    if (child is ContentPresenter)
                    {
                        var contentPresenter = child as ContentPresenter;
                        for (int k = 0; k < System.Windows.Media.VisualTreeHelper.GetChildrenCount(contentPresenter); k++)
                        {
                            var innerChild = System.Windows.Media.VisualTreeHelper.GetChild(contentPresenter, k);
                            if (innerChild.DependencyObjectType.Name == nameControl)
                                return (UserControl)innerChild;
                        }
                    }
                        
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static bool GetPermission(DataTable dtPermission, string ControlName, bool Enable)
        {
            bool flag = Enable;
            try
            {
                foreach (DataRow row in dtPermission.Rows)
                {
                    if (!string.IsNullOrEmpty(row["ControlID"].ToString()) && (row["ControlID"].ToString().ToUpper() == ControlName.ToUpper()))
                    {
                        if ((row["Enable"] == "1") || (row["Enable"].ToString().ToUpper() == "TRUE"))
                        {
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }
            }
            catch
            {
                flag = Enable;
            }
            if (flag)
            {
                flag = Enable;
            }
            return flag;
        }


        #region Hàm dùng cho data Table
        /// <summary>
        /// User:TAMN 21/07/2014
        /// Muc Dich: Dung de gan du lieu tu datadrow vao cac controll
        /// Lưu ý: Có kèm theo nhãn VD:Header or Line
        /// </summary>
        /// <param name="drInfo"></param>
        /// <param name="TagName"></param>
        /// <param name="RootName"></param>
        public static void ElementToControl(DataRow drInfo, string TagName, UIElementCollection RootName)
        {
            SetValueControl(drInfo, TagName, RootName);
            int count = RootName.Count;
            for (int i = 0; i < count; i++)
            {
                try
                {
                    UIElement element = RootName[i];
                    if (element.GetType() == typeof(StackPanel))
                    {
                        SetValueControl(drInfo, TagName, ((StackPanel)element).Children);
                    }
                }
                catch
                {
                }
            }
        }
        /// <summary>
        /// User:TAMNN 21/07/2014
        /// Muc Dich: ham con cua ElementToControl gan du lieu cho 1 controll
        /// Lưu ý: Có kèm theo nhãn VD:Header or Line
        /// </summary>
        /// <param name="drInfo"></param>
        /// <param name="astrTypeTable"></param>
        /// <param name="aUIRoot"></param>
        private static void SetValueControl(DataRow drInfo, string astrTypeTable, UIElementCollection aUIRoot)
        {
            string str = "";
            int count = aUIRoot.Count;
            for (int i = 0; i < count; i++)
            {
                try
                {
                    string name;
                    UIElement element = aUIRoot[i];
                    Type type = element.GetType();
                    if (type == typeof(TextBox))
                    {
                        try
                        {
                            str = ((TextBox)element).Tag.ToString();
                            
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((TextBox)element).Name.Substring(3);
                            ((TextBox)element).Text = "";
                            ((TextBox)element).Text = drInfo[name]!=null ?drInfo[name].ToString(): "";
                        }
                    }
                    else if (type == typeof(AWTextBox))
                    {
                        try
                        {
                            str = ((AWTextBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((AWTextBox)element).Name;
                            string format = "";
                            name = name.Substring(3);
                            ((AWTextBox)element).Text = "";
                            ((AWTextBox)element).Text = drInfo[name]!=null ?drInfo[name].ToString() : "" ;
                        }
                    }
                    else if (type == typeof(TextBlock))
                    {
                        try
                        {
                            str = ((TextBlock)element).Tag.ToString();
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((TextBlock)element).Name.Substring(3);
                            ((TextBlock)element).Text = "";
                            ((TextBlock)element).Text = drInfo[name]!=null ?drInfo[name].ToString(): "" ;
                        }
                    }
                    else if (type == typeof(AWDatePicker))
                    {
                        try
                        {
                            str = ((AWDatePicker)element).Tag.ToString();
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((AWDatePicker)element).Name.Substring(3);
                            ((AWDatePicker)element).Text = "";
                            ((AWDatePicker)element).Text = drInfo[name] != null ? (drInfo[name].ToString()) : "";
                        }
                    }
                    else if (type == typeof(AWCboCheckBox))
                    {
                        try
                        {
                            str = ((AWCboCheckBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((AWCboCheckBox)element).Name.Substring(3);
                            ((AWCboCheckBox)element).Text = "";
                            ((AWCboCheckBox)element).Text = drInfo[name] != null ? (drInfo[name].ToString()) : "";
                        }
                    }
                    else if (type == typeof(CheckBox))
                    {
                        try
                        {
                            str = ((CheckBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((CheckBox)element).Name.Substring(3);
                            string values = drInfo[name] != null ? drInfo[name].ToString() : "";
                            ((CheckBox)element).IsChecked = false;
                            if (values.ToUpper() == "TRUE")
                            {
                                ((CheckBox)element).IsChecked = true;
                            }
                        }
                    }
                    else if (type == typeof(ComboBox))
                    {
                        try
                        {
                            str = ((ComboBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((ComboBox)element).Name.Substring(3);
                            ((ComboBox)element).SelectedValue = drInfo[name] != null ? drInfo[name].ToString() : "";
                        }
                    }
                    else if (type == typeof(ListBox))
                    {
                        try
                        {
                            str = ((ListBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((ListBox)element).Name.Substring(3);
                            ((ListBox)element).SelectedValue = drInfo[name] != null ? drInfo[name].ToString() : "";
                        }
                    }
                    else if (type == typeof(PasswordBox))
                    {
                        try
                        {
                            str = ((PasswordBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str = "";
                        }
                        if (str == astrTypeTable)
                        {
                            name = ((PasswordBox)element).Name.Substring(3);
                            ((PasswordBox)element).Password = drInfo[name] != null ? drInfo[name].ToString() : "";
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Muc Dich: chuyển dữ liệu từ controll về datatable
        /// Lưu ý: Có kèm theo Nhãn VD:Header or Line
        /// </summary>
        /// <param name="drInfoName"></param>
        /// <param name="TagName"></param>
        /// <param name="LayoutRootName"></param>
        public static void ControlToElement(ref DataRow drInfoName, string TagName, UIElementCollection LayoutRootName)
        {
            GetValueFromControl(ref drInfoName, TagName, LayoutRootName);
            int count = LayoutRootName.Count;
            for (int i = 0; i < count; i++)
            {
                try
                {
                    UIElement element = LayoutRootName[i];
                    if (element.GetType() == typeof(StackPanel))
                    {
                        GetValueFromControl(ref drInfoName, TagName, ((StackPanel)element).Children);
                    }
                }
                catch
                {
                }
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Hàm con của ControlToElement Lấy dữ liệu của 1 controll và gán vào datadrow
        /// Lưu ý: Có kèm theo Nhãn VD:Header or Line
        /// </summary>
        /// <param name="drInfo"></param>
        /// <param name="astrTypeTable"></param>
        /// <param name="aUIRoot"></param>
        private static void GetValueFromControl(ref DataRow drInfo, string astrTypeTable, UIElementCollection aUIRoot)
        {
            int count = aUIRoot.Count;
            for (int i = 0; i < count; i++)
            {
                string name = "";
                try
                {
                    string str2;
                    UIElement element = aUIRoot[i];
                    Type type = element.GetType();
                    if (type == typeof(TextBox))
                    {
                            str2 = "";
                            try
                            {
                                str2 = ((TextBox)element).Tag.ToString();
                            }
                            catch
                            {
                                str2 = "";
                            }
                            if (str2 == astrTypeTable)
                            {
                                name = ((TextBox)element).Name;
                                name = name.Substring(3);
                                drInfo[name] = (((TextBox)element).Text == null) ? "" : ((((TextBox)element).Text.Trim() == "") ? "" : ((TextBox)element).Text);
                            }
                    }
                    else if (type == typeof(ComboBox))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((ComboBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((ComboBox)element).Name;
                            name = name.Substring(3);
                            drInfo[name] = (((ComboBox)element).Text == null) ? null : ((((ComboBox)element).Text.ToString().Trim() == "") ? null : ((ComboBox)element).Text.ToString());
                        }
                    }
                    else if (type == typeof(ListBox))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((ListBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((ListBox)element).Name;
                            name = name.Substring(3);
                            drInfo[name] = (((ListBox)element).SelectedItem == null) ? null : ((((ListBox)element).SelectedItem.ToString().Trim() == "") ? null : ((ListBox)element).SelectedItem.ToString());
                        }
                    }
                    else if (type == typeof(TextBlock))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((TextBlock)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((TextBlock)element).Name;
                            name = name.Substring(3);
                            drInfo[name] =  (((TextBlock)element).Text == null) ? null : ((((TextBlock)element).Text.Trim() == "") ? null : ((TextBlock)element).Text);
                        }
                    }
                    else if (type == typeof(AWTextBox))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((AWTextBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((AWTextBox)element).Name;
                            name = name.Substring(3);
                            drInfo[name] = (((AWTextBox)element).Text == null) ? null : ((((AWTextBox)element).Text.Trim() == "") ? null : ((AWTextBox)element).Text);
                        }
                    }
                    else if (type == typeof(AWDatePicker))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((AWDatePicker)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((AWDatePicker)element).Name;
                            name = name.Substring(3);
                            drInfo[name] = (((AWDatePicker)element).Text == null) ? null : ((((AWDatePicker)element).Text.Trim() == "") ? null : LibraryFormatConverter.ConvertStringDDMMYYYYToStringYYYYMMDD(((AWDatePicker)element).Text));
                        }
                    }
                    else if (type == typeof(AWCboCheckBox))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((AWCboCheckBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((AWCboCheckBox)element).Name;
                            name = name.Substring(3);
                            drInfo[name] = (((AWCboCheckBox)element).aaCode == null) ? null : ((((AWCboCheckBox)element).aaCode.Trim() == "") ? null : ((AWCboCheckBox)element).aaCode);
                        }
                    }
                    else if (type == typeof(CheckBox))
                    {
                        str2 = "";
                        try
                        {
                            str2 = ((CheckBox)element).Tag.ToString();
                        }
                        catch
                        {
                            str2 = "";
                        }
                        if (str2 == astrTypeTable)
                        {
                            name = ((CheckBox)element).Name;
                            name = name.Substring(3);
                            string str3 = "False";
                            if (((CheckBox)element).IsChecked == true)
                            {
                                str3 = "True";
                            }
                            drInfo[name] = str3;
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw new Exception(exception.Message + ": " + name);
                }
            }
        }
        /// <summary>
        /// user:TAMNN ; 18/07/2014
        /// Mục đích: chuyển dữ liệu từ controll về datatable
        /// </summary>
        /// <param name="drInfoName"></param>
        /// <param name="TagName"></param>
        /// <param name="LayoutRootName"></param>
        public static void ControlToElement(ref DataRow drInfoName, UIElementCollection LayoutRootName)
        {
            GetValueFromControl(ref drInfoName,LayoutRootName);
            int count = LayoutRootName.Count;
            for (int i = 0; i < count; i++)
            {
                try
                {
                    UIElement element = LayoutRootName[i];
                    if (element.GetType() == typeof(StackPanel))
                    {
                        GetValueFromControl(ref drInfoName, ((StackPanel)element).Children);
                    }
                }
                catch
                {
                }
            }
        }
        /// <summary>
        /// user:TAMNN ; 18/07/2014
        /// Mục đích: Hàm con của ControlToElement Lấy dữ liệu của 1 controll và gán vào datadrow
        /// </summary>
        /// <param name="drInfo"></param>
        /// <param name="astrTypeTable"></param>
        /// <param name="aUIRoot"></param>
        private static void GetValueFromControl(ref DataRow drInfo, UIElementCollection aUIRoot)
        {
            int count = aUIRoot.Count;
            for (int i = 0; i < count; i++)
            {
                string name = "";
                try
                {
                    UIElement element = aUIRoot[i];
                    Type type = element.GetType();
                    if (type == typeof(TextBox))
                    {
                        if (((TextBox)element).Tag != null && !string.IsNullOrEmpty(((TextBox)element).Tag.ToString()))
                        {
                            name = ((TextBox)element).Tag.ToString();
                            drInfo[name] = (((TextBox)element).Text == null) ? null : ((((TextBox)element).Text.Trim() == "") ? null : ((TextBox)element).Text);
                        }
                    }
                    else if (type == typeof(ComboBox))
                    {
                        if (((ComboBox)element).Tag != null && !string.IsNullOrEmpty(((ComboBox)element).Tag.ToString()))
                        {
                            name = ((ComboBox)element).Tag.ToString();
                            drInfo[name] = (((ComboBox)element).Text == null) ? null : ((((ComboBox)element).Text.ToString().Trim() == "") ? null : ((ComboBox)element).Text.ToString());
                        }
                    }
                    else if (type == typeof(ListBox))
                    {
                        if (((ListBox)element).Tag != null && !string.IsNullOrEmpty(((ListBox)element).Tag.ToString()))
                        {
                            name = ((ListBox)element).Tag.ToString();
                            drInfo[name] = (((ListBox)element).SelectedItem == null) ? null : ((((ListBox)element).SelectedItem.ToString().Trim() == "") ? null : ((ListBox)element).SelectedItem.ToString());
                        }
                    }
                    else if (type == typeof(TextBlock))
                    {
                        if (((TextBlock)element).Tag != null && !string.IsNullOrEmpty(((TextBlock)element).Tag.ToString()))
                        {
                            name = ((TextBlock)element).Tag.ToString();
                            drInfo[name] = (((TextBlock)element).Text == null) ? null : ((((TextBlock)element).Text.Trim() == "") ? null : ((TextBlock)element).Text);
                        }
                    }
                    else if (type == typeof(AWTextBox))
                    {
                        if (((AWTextBox)element).Tag != null && !string.IsNullOrEmpty(((AWTextBox)element).Tag.ToString()))
                        {
                            name = ((AWTextBox)element).Tag.ToString();
                            drInfo[name] = (((AWTextBox)element).Text == null) ? null : ((((AWTextBox)element).Text.Trim() == "") ? null : ((AWTextBox)element).Text);
                        }
                    }
                    else if (type == typeof(AWDatePicker))
                    {
                        if (((AWDatePicker)element).Tag!= null && !string.IsNullOrEmpty(((AWDatePicker)element).Tag.ToString()))
                        {
                            name = ((AWDatePicker)element).Tag.ToString();
                            drInfo[name] = LibraryFormatConverter.ConvertStringDDMMYYYYToStringYYYYMMDD(((AWDatePicker)element).Text);
                        }
                    }
                    else if (type == typeof(CheckBox))
                    {
                        if (((CheckBox)element).Tag!= null && !string.IsNullOrEmpty(((CheckBox)element).Tag.ToString()))
                        {
                            name = ((CheckBox)element).Tag.ToString();
                            string str3 = "False";
                            if (((CheckBox)element).IsChecked == true)
                            {
                                str3 = "True";
                            }
                            drInfo[name] = str3;
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw new Exception(exception.Message + ": " + name);
                }
            }
        }
        /// <summary>
        /// User:TAMN 21/07/2014
        /// Mục đích: dùng để gán dữ liệu từ datadrow vào các controll
        /// </summary>
        /// <param name="drInfo"></param>
        /// <param name="TagName"></param>
        /// <param name="RootName"></param>
        public static void ElementToControl(DataRow drInfo, UIElementCollection RootName)
        {
            SetValueControl(drInfo, RootName);
            int count = RootName.Count;
            for (int i = 0; i < count; i++)
            {
                try
                {
                    UIElement element = RootName[i];
                    if (element.GetType() == typeof(StackPanel))
                    {
                        SetValueControl(drInfo, ((StackPanel)element).Children);
                    }
                }
                catch
                {
                }
            }
        }
        /// <summary>
        /// User:TAMNN 21/07/2014
        /// Mục đích: hàm con của ElementToControl gán dữ liệu cho 1 controll
        /// </summary>
        /// <param name="drInfo"></param>
        /// <param name="astrTypeTable"></param>
        /// <param name="aUIRoot"></param>
        private static void SetValueControl(DataRow drInfo, UIElementCollection aUIRoot)
        {
            string str = "";
            int count = aUIRoot.Count;
            for (int i = 0; i < count; i++)
            {
                try
                {
                    string name;
                    UIElement element = aUIRoot[i];
                    Type type = element.GetType();
                    if (type == typeof(TextBox))
                    {
                        if (((TextBox)element).Tag != null && !string.IsNullOrEmpty(((TextBox)element).Tag.ToString()))
                        {
                            name = ((TextBox)element).Tag.ToString();
                            ((TextBox)element).Text = "";
                            ((TextBox)element).Text = drInfo[name] != null ? drInfo[name].ToString() : "";
                        }
                    }
                    else if (type == typeof(AWTextBox))
                    {
                        if (((AWTextBox)element).Tag != null && !string.IsNullOrEmpty(((AWTextBox)element).Tag.ToString()))
                        {
                            name = ((AWTextBox)element).Tag.ToString();
                            ((AWTextBox)element).Text = "";
                            ((AWTextBox)element).Text = drInfo[name] != null ? drInfo[name].ToString() : "";
                        }
                    }
                    else if (type == typeof(TextBlock))
                    {
                        if (((TextBlock)element).Tag != null && !string.IsNullOrEmpty(((TextBlock)element).Tag.ToString()))
                        {
                            name = ((TextBlock)element).Tag.ToString();
                            ((TextBlock)element).Text = "";
                            ((TextBlock)element).Text = drInfo[name] != null ? drInfo[name].ToString() : "";
                        }
                    }
                    else if (type == typeof(AWDatePicker))
                    {
                        if (((AWDatePicker)element).Tag != null && !string.IsNullOrEmpty(((AWDatePicker)element).Tag.ToString()))
                        {
                            name = ((AWDatePicker)element).Tag.ToString();
                            ((AWDatePicker)element).Text = "";
                            ((AWDatePicker)element).Text = drInfo[name] != null ? (Convert.ToDateTime(drInfo[name].ToString())).ToString("dd/MM/yyyy") : "";
                        }
                    }
                    else if (type == typeof(CheckBox))
                    {
                        if (((CheckBox)element).Tag != null && !string.IsNullOrEmpty(((CheckBox)element).Tag.ToString()))
                        {
                            name = ((CheckBox)element).Tag.ToString();
                            string values = drInfo[name] != null ? drInfo[name].ToString() : "";
                            ((CheckBox)element).IsChecked = false;
                            if (values.ToUpper() == "TRUE")
                            {
                                ((CheckBox)element).IsChecked = true;
                            }
                        }
                    }
                    else if (type == typeof(ComboBox))
                    {
                    //    if (((ComboBox)element).Tag != null && !string.IsNullOrEmpty(((ComboBox)element).Tag.ToString()))
                    //    {
                    //        name = ((ComboBox)element).Tag.ToString();
                    //        ((ComboBox)element).Text = drInfo[name] != null ? drInfo[name].ToString() : "";
                    //    }
                    }
                    else if (type == typeof(ListBox))
                    {
                        if (((ListBox)element).Tag != null && !string.IsNullOrEmpty(((ListBox)element).Tag.ToString()))
                        {
                            name = ((ListBox)element).Tag.ToString();
                            ((ListBox)element).SelectedValue = drInfo[name] != null ? drInfo[name].ToString() : "";
                        }
                    }
                    else if (type == typeof(PasswordBox))
                    {
                        if (((PasswordBox)element).Tag != null && !string.IsNullOrEmpty(((PasswordBox)element).Tag.ToString()))
                        {
                            name = ((PasswordBox)element).Tag.ToString();
                            ((PasswordBox)element).Password = drInfo[name] != null ? drInfo[name].ToString() : "";
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Thêm dòng theo vị trí cho table
        /// </summary>
        /// <param name="dtTable"></param>
        /// <param name="AtRow"></param>
        public static void AddRowDataGrid(ref DataTable dtTable, int AtRow)
        {
            DataRow dr = dtTable.NewRow();
            dtTable.Rows.InsertAt(dr, AtRow);
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Thêm dòng theo vị trí cho table
        /// </summary>
        /// <param name="dtTable"></param>
        /// <param name="AtRow"></param>
        public static void AddRowInTable(ref DataTable dtTable, int SoDong)
        {
            for (int i = 0; i < SoDong; i++)
            {
                DataRow dr = dtTable.NewRow();
                dtTable.Rows.Add(dr);
            }
        }

        public static void checkDataRow(ref DataRow dr, ref string message)
        {
 
        }
        #endregion

        /// <summary>
        /// mode form
        /// </summary>
        public enum FormMode
        {
            NewMode,
            EditMode,
            SaveMode,
            LoadMode,
            UpdataMode,
            ReadOnlyMode
        }

        /// <summary>
        /// FormName
        /// </summary>
        public enum FormName
        {
            HT_DM_BoPhan
        }

        /// <summary>
        /// Ma tien to
        /// </summary>
        public enum Counter
        {
            BP,
            DV,
            QT,
            M,
            RS,
            NV,
            R,
            TB,
            TG

        }
    }
}
